<template>
  <!-- 顾客管理 -->
  <div class="custome">
    <!-- 头部 -->
    <div class="header">
      <!-- 添加按钮 -->
      <el-button type="primary" @click="addCustomer">添加</el-button>
      <!-- 用户名输入框 -->
      <el-input
        v-model="customerName"
        style="width: 200px"
        placeholder="请输入用户名"
        clearable
      />
      <!-- 状态下拉框 -->
      <el-select v-model="customerStatus" clearable placeholder="请选择状态">
        <el-option label="正常" value="正常" />
        <el-option label="禁用" value="禁用" />
      </el-select>
      <!-- 查询按钮 -->
      <el-button type="primary" @click="queryCustomer">查询</el-button>
    </div>
    <!-- 主体内容 -->
    <div class="main">
      <!-- 表格 -->
      <el-table :data="customerData" style="width: 100%">
        <!-- 编号 -->
        <el-table-column fixed prop="id" label="编号" align="center" />
        <!-- 用户名 -->
        <el-table-column prop="username" label="用户名" align="center" />
        <!-- 姓名 -->
        <el-table-column prop="realname" label="姓名" align="center" />
        <!-- 手机号 -->
        <el-table-column prop="telephone" label="手机号" align="center" />
        <!-- 性别 -->
        <el-table-column prop="gender" label="性别" align="center">
          <template slot-scope="scope">
            <i
              v-if="scope.row.gender === 'male'"
              class="el-icon-male green_info"
            />
            <i v-else class="el-icon-female red_info" />
          </template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column prop="status" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              size="mini"
              :type="scope.row.status === '正常' ? 'success' : 'danger'"
            >
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="editCustomer(scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              size="small"
              style="color: #e76676"
              @click="deleteframe(scope.row.id)"
            >删除</el-button>
            <el-button
              type="text"
              size="small"
              style="color: #618db9"
              @click.prevent="userInfoJump(scope.row.id)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 底部分页 -->
    <div class="footer">
      <!-- 分页组件 -->
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[4, 6, 8, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 模态框 -->
    <el-dialog
      :title="dialogtitle"
      :visible.sync="dialogVisible"
      @close="dialogClose"
    >
      <!-- 表单（模态框） -->
      <el-form
        ref="customer_form"
        :model="customerForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="customerForm.username" />
        </el-form-item>
        <!-- 姓名 -->
        <el-form-item label="姓名" prop="realname">
          <el-input v-model="customerForm.realname" />
        </el-form-item>
        <!-- 手机号 -->
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model="customerForm.telephone" />
        </el-form-item>
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input v-model="customerForm.password" />
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="customerForm.gender">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item v-if="statusVisible" label="状态" prop="status">
          <el-radio-group v-model="customerForm.status">
            <el-radio label="正常" />
            <el-radio label="禁用" />
          </el-radio-group>
        </el-form-item>
      </el-form>
      <!-- 模态框底部 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm(statusVisible)"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除的模态框 -->
    <el-dialog title="提示" :visible.sync="deleteVisible">
      <span class="el-icon-warning" />
      <span>该操作将永久的删除这个用户数据，是否继续?</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deletecancel">取 消</el-button>
        <el-button
          type="primary"
          @click="deleteCustomer(deleteCustomerId)"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 引入axios
import { get, post } from '@/utils/request'
// 引入vuex的辅助函数
import { mapActions, mapState } from 'vuex'
export default {
  data() {
    return {
      // 用户数据
      customerData: [],
      // 当前页
      currentPage: 1,
      // 每页展示的条数
      pageSize: 8,
      // 总页数
      total: 10,
      // 用户名
      customerName: '',
      // 顾客状态
      customerStatus: '',
      // 查询时的对象
      queryData: {},
      // 模态框标题
      dialogtitle: '',
      // 删除记录的用户ID
      deleteCustomerId: {},
      // 删除显示模态框
      deleteVisible: false,
      // 是否显示模态框
      dialogVisible: false,
      // 是否显示状态栏
      statusVisible: false,
      // 模态框表单对象
      customerForm: {
        // 用户名
        username: '',
        // 姓名
        realname: '',
        // 手机号
        telephone: '',
        // 密码
        password: '',
        // 性别
        gender: '',
        // 状态
        status: ''
      },
      // 表单验证规则
      rules: {
        // 用户名
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        // 姓名
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        // 电话
        telephone: [
          { required: true, message: '请输入电话号码', trigger: 'blur' }
          // { type: "number", message: "电话必须都是数字"},
        ],
        // 密码
        password: [{ required: true, message: '请选择密码', trigger: 'blur' }],
        // 性别
        gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        // 状态
        status: [{ required: true, message: '请选择状态', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {
    // 自动获取用户数据
    this.getAllCustomeData()
  },
  mounted() {},
  methods: {
    // 获取所有用户信息
    async getAllCustomeData() {
      const data = {
        page: this.currentPage,
        pageSize: this.pageSize
      }
      const res = await get('/baseUser/pageQuery', {
        ...data,
        ...this.queryData,
        roleId: 6
      })
      // console.log(res);
      this.customerData = res.data.list
      this.total = res.data.total
    },
    // 控制每页显示多少条
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getAllCustomeData()
    },
    // 控制当前页数
    handleCurrentChange(val) {
      this.currentPage = val
      this.getAllCustomeData()
    },
    // 查询用户数据
    queryCustomer() {
      const data = {
        username: this.customerName,
        status: this.customerStatus,
        roleId: 6
      }
      for (const key in data) {
        if (!data[key]) {
          delete data[key]
        }
      }
      this.queryData = data
      this.currentPage = 1
      this.getAllCustomeData()
    },
    // 添加按钮
    addCustomer() {
      this.customerForm = {}
      this.dialogtitle = '添加顾客信息'
      this.statusVisible = false
      this.dialogVisible = true
    },
    // 编辑按钮
    editCustomer(row) {
      this.customerForm = { ...row }
      this.dialogtitle = '编辑顾客信息'
      this.statusVisible = true
      this.dialogVisible = true
    },
    // 删除按钮调用模态框
    deleteframe(id) {
      this.deleteCustomerId = id
      this.deleteVisible = true
    },
    // 取消删除
    deletecancel() {
      this.deleteVisible = false
      this.$message('已取消删除')
    },
    // 删除用户
    async deleteCustomer(id) {
      await get('/baseUser/deleteById', { id })
      this.getAllCustomeData()
      this.deleteVisible = false
      this.$message({
        message: '用户删除成功',
        type: 'success'
      })
      this.getAllCustomeData
    },

    // 表单提交 更新或编辑产品信息
    submitForm(visible) {
      if (!visible) {
        this.$refs['customer_form'].validate(async(valid) => {
          if (valid) {
            // 发送表单验证的请求 提交表单对象
            const data = { ...this.customerForm, roleId: 6, status: '正常' }
            const res = await post('/baseUser/addUserWidthRole', data)
            // 给用户一个成功的提示
            if (res.status == 200) {
              this.$message({
                message: res.message,
                type: 'success'
              })
              // 重新刷新页面的数据
              this.getAllCustomeData()
              // 调用关闭模态框的方法
              this.dialogClose()
            } else {
              this.$message.error(res.message)
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        this.$refs['customer_form'].validate(async(valid) => {
          if (valid) {
            // 发送表单验证的请求 提交表单对象
            const res = await post('/baseUser/saveOrUpdate', this.customerForm)
            // 给用户一个成功的提示
            if (res.status == 200) {
              this.$message({
                message: res.message,
                type: 'success'
              })
              // 重新刷新页面的数据
              this.getAllCustomeData()
              // 调用关闭模态框的方法
              this.dialogClose()
            } else {
              this.$message.error(res.message)
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    },
    // 详情界面跳转
    userInfoJump(id) {
      this.$router.push({
        path: '/admin/custome/Details',
        query: { id }
      })
    },

    // 关闭模态框 调用表单重置的方法
    dialogClose() {
      // 重置表单
      this.$refs['customer_form'].resetFields()
      // 关闭模态框
      setTimeout(() => {
        this.dialogVisible = false
      }, 200)
    }
  }
}
</script>
<style scoped>
.header {
  line-height: 40px;
  margin-left: 10px;
  margin-bottom: 20px;
}
.footer {
  position: fixed;
  right: 10px;
  bottom: 20px;
}
.el-icon-warning {
  display: inline-block;
  color: red;
  font-size: 20px;
  margin-right: 5px;
  padding-top: 5px;
}
</style>
